﻿using LxBasic;

namespace LxCore
{
    public abstract class TemperatureSolver
    {
        public class Set : IterateSolver.Set<Unit>
        {
            public override void CheckCreate()
            {
                base.CheckCreate();
                var list = new ConnectivitySolver().GetConnectivity(f => f.Temperature, c => c.Temperature);
                Solvers = new Unit[list.Count];
                for (int i1 = 0; i1 < Solvers.Length; i1++)
                {
                    var p = list[i1];
                    Solvers[i1] = new Unit()
                    {
                        Equations = p.Equations.ToArray(),
                        Connects = p.Connects.ToArray(),
                        InSolveSet = this,
                    };
                }
            }
        }
        public class Unit : IterateSolver.Unit
        {
            public EquationWithTransTerm[] Equations;
            public EquationConnectBase[] Connects;
            public Set InSolveSet;
            protected void post()
            {
                UpdateBoundsValue(Equations, Connects);
                Limit(Equations, Connects);
                CalGradient(Equations, Connects);
                CalResidual(Equations, Connects);
            }
            public override void SaveOldValue()
            {
                foreach (var s in Equations) { s.TransData.SaveOldValue(); }
            }
            public override void Solve_Steady()
            {
                FormMatrix_SteadyPart(Equations, Connects);
                Relax(Equations, Connects);
                Solve(Equations, Connects); 
                post();
            }
            public override void Solve_ExplicitTrans()
            {
                FormMatrix_SteadyPart(Equations, Connects);
                FormMatrix_ExplicitTrans(Equations, Connects);
                SolveExplicit(Equations, Connects);
                post();
            }
            public override void Solve_ImplicitTrans()
            {
                FormMatrix_SteadyPart(Equations, Connects);
                FormMatrix_ImplicitTrans(Equations, Connects);
                Relax(Equations, Connects);
                Solve(Equations, Connects);
                post();
            }
            public override void Initialize()
            {
                Initialize(Equations);
            }
        }
    }

}
